package com.sys.controller.monitor;

import com.sys.common.config.SysConfigProperties;
import com.sys.common.constant.Constants;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.utils.StringUtils;
import com.sys.framework.web.domain.Server;
import com.sys.system.domain.SysLoginInfo;
import com.sys.system.domain.SysNotice;
import com.sys.system.service.ISysLoginInfoService;
import com.sys.system.service.ISysNoticeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页
 *
 * @author LGY
 */
@Tag(name = "首页API")
@RestController
@RequestMapping("/index")
public class SysIndexController {
    /**
     * 系统基础配置
     */
    @Autowired
    private SysConfigProperties sysConfig;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private ISysLoginInfoService loginInfoService;

    /**
     * 访问首页，提示语
     */
    @Operation(summary = "访问首页，提示语")
    @RequestMapping("/")
    public String index() {
        return StringUtils.format("欢迎您");
    }

    @Operation(summary = "获取服务器信息")
    @GetMapping("/server")
    public AjaxResult getIndexServer() throws Exception {
        Server server = new Server();
        server.copyTo();
        return AjaxResult.success(server);
    }

    @Operation(summary = "获取通知列表")
    @GetMapping("/notice")
    public AjaxResult getIndexNotice() {
        SysNotice query = new SysNotice();
        List<SysNotice> list = noticeService.selectNoticeList(query);
        return AjaxResult.success(list);
    }

    @Operation(summary = "获取通知详情")
    @GetMapping("/notice/detail/{id}")
    public AjaxResult getIndexNoticeDetail(@PathVariable String id) {
        SysNotice notice = noticeService.getById(id);
        return AjaxResult.success(notice);
    }

    @Operation(summary = "获取当天登录人数统计")
    @GetMapping("/loginCount")
    public AjaxResult loginCount() {
        SysLoginInfo sysLoginInfo = new SysLoginInfo();
        sysLoginInfo.setLoginTime(new Date());
        List<SysLoginInfo> loginInfoList = loginInfoService.findLogininforList(sysLoginInfo);
        // 横坐标
        List<String> xAxis = new ArrayList<>();
        // 登录成功统计
        List<Integer> yAxis = new ArrayList<>();
        // 登录失败统计
        List<Integer> yAxis2 = new ArrayList<>();
        // 退出统计
        List<Integer> yAxis3 = new ArrayList<>();
        // 注册统计
        List<Integer> yAxis4 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(loginInfoList)) {
            Map<Integer, List<SysLoginInfo>> hourMap = loginInfoList.stream().collect(Collectors.groupingBy(item -> {
                LocalDateTime localDateTime = item.getLoginTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                return localDateTime.getHour();
            }));
            for (int targetHour = 0; targetHour < 24; targetHour++) {
                xAxis.add(targetHour + "点");
                // 目标时间的数据
                List<SysLoginInfo> sysLoginInfoList = hourMap.get(targetHour);
                // 登录成功
                int yCount = 0;
                // 登录失败
                int yCount2 = 0;
                // 退出
                int yCount3 = 0;
                // 注册
                int yCount4 = 0;
                if (!CollectionUtils.isEmpty(sysLoginInfoList)) {
                    for (SysLoginInfo item : sysLoginInfoList) {
                        if (Objects.equals(item.getType(), 0)) {
                            // 登录
                            if (Objects.equals(item.getStatus(), Constants.SUCCESS)) {
                                yCount = yCount + 1;
                            } else {
                                yCount2 = yCount2 + 1;
                            }
                        } else if (Objects.equals(item.getType(), 1)) {
                            yCount3 = yCount3 + 1;
                        } else if (Objects.equals(item.getType(), 2)) {
                            yCount4 = yCount4 + 1;
                        }
                    }
                }
                yAxis.add(yCount);
                yAxis2.add(yCount2);
                yAxis3.add(yCount3);
                yAxis4.add(yCount4);
            }
        }
        AjaxResult res = AjaxResult.success();
        res.put("x", xAxis);
        res.put("y", yAxis);
        res.put("y2", yAxis2);
        res.put("y3", yAxis3);
        res.put("y4", yAxis4);
        return res;
    }


    @Operation(summary = "获取登录记录")
    @GetMapping("/findLoginList")
    public AjaxResult findLoginList() {
        SysLoginInfo sysLoginInfo = new SysLoginInfo();
        sysLoginInfo.setLoginTime(new Date());
        List<SysLoginInfo> loginInfoList = loginInfoService.findLogininforList(sysLoginInfo);
        return AjaxResult.success(loginInfoList);
    }
}
